package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.uiUtils.ScrollSmoothLineaerLayoutManager;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.HistoryCallnameAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.TongjiInfoAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 统计详情界面
 *
 * @author yingchunli
 */
public class TongjiInfoActivity extends BaseSoocActivity {

    @InjectView(R.id.className_tongji)
    TextView kcmc;//课程名称
    @InjectView(R.id.dmrs_quexitongji)
    TextView qdcs;//点名次数
    @InjectView(R.id.classTime_tongji)
    TextView skxq;//上课时间
    @InjectView(R.id.list_tongji)
    UltimateRecyclerView list_tongji;//列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    TongjiInfoAdapter adapter;//适配器
    CallNameDao dao;
    String kcid;//课程id
    int maxLastPositon;//数据最后位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_tongjiinfo);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        //初始化listView
        linearLayoutManager = new LinearLayoutManager(this);
        list_tongji.setLayoutManager(linearLayoutManager);

        //给课程名称  上课日期  点名次数 赋值
        kcmc.setText(getIntent().getStringExtra("kcmc"));
        qdcs.setText(getIntent().getStringExtra("qdcs"));
        skxq.setText(getIntent().getStringExtra("skxq"));

        kcid = getIntent().getStringExtra("kcid");
        //发送请求  获取列表数据
        Log.d("kcid——》" + kcid);
        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        dao.getCourseInfo(kcid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJIINFO_FRIST);
    }

    /**
     * 初始化数据
     */
    void initView() {
        adapter = new TongjiInfoAdapter(dao.getClassInfoList(), this);
        list_tongji.setAdapter(adapter);
        list_tongji.enableLoadmore();
        list_tongji.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PageConst.pageNo = 1;
                dao.getCourseInfo(kcid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJIINFO_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        list_tongji.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getCourseInfo(kcid, ++PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJIINFO_PULLUP);
                } else {
                    adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
        adapter.setmOnItemClickLitener(new TongjiInfoAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, ClassInfo classInfo) {
                Intent intent = new Intent(TongjiInfoActivity.this, SignInfoActivity.class);
                intent.putExtra("ktid", classInfo.getKtid());
                intent.putExtra("kcid", kcid);
                intent.putExtra("pzqd", classInfo.getPzqd() + "");
                startActivityForResult(intent, 0);
                AnimUtil.intentSlidIn(TongjiInfoActivity.this);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        dao.getCourseInfo(kcid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_GET_TONGJIINFO_PUSHDOWN);
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_GET_TONGJIINFO_FRIST:
                        //查看详情成功  首次请求
                        initView();
                        break;
                    case CodeConst.EXPAND_GET_TONGJIINFO_PUSHDOWN:
                        //下拉刷新
                        list_tongji.setRefreshing(false);
                        adapter.refresh(dao.getClassInfoList());
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {

                        }
                        if (dao.getClassInfoList().size() > (PageConst.pageSize - 1)) {
                            adapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    case CodeConst.EXPAND_GET_TONGJIINFO_PULLUP:
                        //上拉加载
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            adapter.loadmore(dao.getClassInfoList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "统计详情";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    /**
     * Toolbar的左侧按钮点击
     */
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }
}
